package com.assets;

import android.os.Bundle;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.assets.Fragment.Allot;
import com.assets.Fragment.Fragment1;
import com.assets.Fragment.Fragment2;
import com.assets.Fragment.Fragment3;
import com.assets.Fragment.Fragment4;
import com.assets.Fragment.Fragment5;
import com.assets.Fragment.Fragment6;
import com.assets.Fragment.Fragment7;
import com.assets.Fragment.Repairs;
import com.assets.Fragment.Scrap;
import com.assets.menu.Menu_Adapter;
import com.assets.menu.Menu_bean;
import com.assets.room.database.MyDataBase;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends FragmentActivity implements OnItemClickListener {


    public DrawerLayout drawerLayout;
    private ListView listView;
    private List<Menu_bean> linkMains = new ArrayList<>();
    private FragmentManager fragmentManager;
    private Fragment fragment1, fragment2, fragment3, fragment4, fragment5, fragment6, fragment7, Allot, Scrap, Repairs;
    private String[] str;
    private MyDataBase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    public void initView() {

//        new Thread(() -> {


//
//        }).start();

        // TODO Auto-generated method stub
        drawerLayout = (DrawerLayout) findViewById(R.id.id_drawerlayout);
        drawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        });

        init();
        listView = (ListView) findViewById(R.id.listview);
        //    str = new String[] { "首页", "入库", "领用","盘点","租赁","归还","转移","调拨","报废","报修"};
        Menu_Adapter adapter = new Menu_Adapter(this,
                R.layout.munu_item, linkMains);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        fragmentManager = getSupportFragmentManager();
        fragment1 = new Fragment1();//首页
//        fragment2 = new Fragment2();
        fragment3 = new Fragment3();//领用页面
        fragment4 = new Fragment4();//盘点页面
//        fragment5 = new Fragment5();
//        fragment6 = new Fragment6();
//        fragment7 = new Fragment7();
//        Allot = new Allot();
//        Scrap = new Scrap();
//        Repairs = new Repairs();
        fragmentManager.beginTransaction()
                .replace(R.id.id_framelayout2, fragment1).commit();
        //drawerLayout.openDrawer(Gravity.LEFT);//侧滑打开  不设置则不会打开

    }

    private void init() {
        linkMains.add(new Menu_bean(R.mipmap.main, "首页"));
//        linkMains.add(new Menu_bean(R.mipmap.main, "入库"));
        linkMains.add(new Menu_bean(R.mipmap.main, "领用"));
        linkMains.add(new Menu_bean(R.mipmap.main, "盘点"));
//        linkMains.add(new Menu_bean(R.mipmap.main, "租赁"));
//        linkMains.add(new Menu_bean(R.mipmap.main, "归还"));
//        linkMains.add(new Menu_bean(R.mipmap.main, "转移"));
//        linkMains.add(new Menu_bean(R.mipmap.main, "调拨"));
//        linkMains.add(new Menu_bean(R.mipmap.main, "报废"));
//        linkMains.add(new Menu_bean(R.mipmap.main, "报修"));
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        // TODO Auto-generated method stub
        switch (position) {
            case 0:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment1).commit();
                break;
//            case 1:
//                fragmentManager.beginTransaction()
//                        .replace(R.id.id_framelayout2, fragment2).commit();
//                break;
            case 2:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment3).commit();
                break;
            case 3:
                fragmentManager.beginTransaction()
                        .replace(R.id.id_framelayout2, fragment4).commit();
                break;
//            case 4:
//                fragmentManager.beginTransaction()
//                        .replace(R.id.id_framelayout2, fragment5).commit();
//                break;
//            case 5:
//                fragmentManager.beginTransaction()
//                        .replace(R.id.id_framelayout2, fragment6).commit();
//                break;
//            case 6:
//                fragmentManager.beginTransaction()
//                        .replace(R.id.id_framelayout2, fragment7).commit();
//                break;
//            case 7:
//                fragmentManager.beginTransaction()
//                        .replace(R.id.id_framelayout2, Allot).commit();
//                break;
//            case 8:
//                fragmentManager.beginTransaction()
//                        .replace(R.id.id_framelayout2, Scrap).commit();
//                break;
//            case 9:
//                fragmentManager.beginTransaction()
//                        .replace(R.id.id_framelayout2, Repairs).commit();
//                break;
        }
        //  setTitle(linkMains.get(linkMains.size()));
        drawerLayout.closeDrawers();
    }

    private long mExitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                //大于2000ms则认为是误操作，使用Toast进行提示
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();
            } else {
                //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    // @Override
    //  protected void onSaveInstanceState(Bundle outState) {
    //       super.onSaveInstanceState(outState);
    //}
    //  protected void onSaveInstanceState(Bundle outState) {           //super.onSaveInstanceState(outState);
    // 注释掉该方法， 即不保存状态
    //   }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}

